package com.starmark.core.framework.converter;

import ma.glasnost.orika.MappingContext;
import ma.glasnost.orika.converter.BidirectionalConverter;
import ma.glasnost.orika.metadata.Type;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author starmark
 * @since 2020/7/16 001620:56
 **/
public class ListToStringConverter extends BidirectionalConverter<List<String>, String> {


    @Override
    public String convertTo(List<String> source, Type<String> destinationType, MappingContext mappingContext) {
        return String.join(";", source);
    }

    @Override
    public List<String> convertFrom(String source, Type<List<String>> destinationType, MappingContext mappingContext) {
        return Arrays.stream(source.split(";")).filter(StringUtils::isNotEmpty).collect(Collectors.toList());

    }
}
